//
// Created by 高森森 on 2022/10/16.
//

#ifndef LEETCODE_SOLUTION_25_H
#define LEETCODE_SOLUTION_25_H
#include<bits/stdc++.h>
using namespace std;

class solution_25 {
public:
    int direction[8][2]={{-1,0},{1,0},{0,-1},{0,1},{-1,-1},{1,1},{-1,1},{1,-1}};
    void gameOfLife(vector<vector<int>>& board) {
        vector<pair<int,int>>result;
        int count=0;
        int x,y,nx,ny;
        for(int i=0;i<board.size();i++)
        {
            for(int j=0;j<board[0].size();j++){
                count=0;
                x=i;
                y=j;
                for(int k=0;k<8;k++){
                     nx=i+direction[k][0];
                     ny=j+direction[k][1];
                    if(nx>=0&&nx<board.size()&&ny>=0&&ny<board[0].size()){
                        if(board[nx][ny]){
                            count++;
                        }
                    }
                }
                if(board[x][y]==1){
                    if(count<2||count>3){
                        result.push_back(make_pair(x,y));
                    }
                }
                if(board[x][y]==0&&count==3){
                    result.push_back(make_pair(x,y));
                }

            }
                    }

        //遍历修改
        for(int i=0;i<board.size();i++){
            for(int j=0;j<board[0].size();j++){
                if(find(result.begin(),result.end(), make_pair(i,j))!=result.end()){
                    if(board[i][j]==0){
                        board[i][j]=1;
                    }else{
                        board[i][j]=0;
                    }
                    cout<<board[i][j];
                }
            }
        }
    }
};


#endif //LEETCODE_SOLUTION_25_H
